#include "stdafx.h"
#include "DatabasePage.h"
#include "ui_MainWindow.h"
#include "Config.h"

using namespace std;
using namespace AirCrash::Calypso;

DatabasePage::DatabasePage() : mUi(0), mPopupImage(0), mItemDb(false) {
}

DatabasePage::~DatabasePage() {
}

void DatabasePage::keyPressEvent(QKeyEvent * e) {
}

void DatabasePage::paintEvent(QPaintEvent * e) {
}

void DatabasePage::showEvent(QShowEvent * e) {
	if (mUi) {
		mUi->mMenuBar->setVisible(true);
	}
}

void DatabasePage::hideEvent(QHideEvent * e) {
	if (mUi) {
		QAbstractItemModel * model = mUi->mDatabaseView->model();
		if (model) {
			delete model;
			mUi->mDatabaseView->setModel(0);
		}
	}
}

void DatabasePage::setUi(Ui::MainWindowClass * ui) {
	mUi = ui;

	connect( mUi->mDatabaseItems, SIGNAL(clicked()), this, SLOT(changeDB()) );
	connect( mUi->mDatabaseFriday, SIGNAL(clicked()), this, SLOT(changeDB()) );
	connect( mUi->mDatabaseSaturday, SIGNAL(clicked()), this, SLOT(changeDB()) );
	connect( mUi->mDatabaseSunday, SIGNAL(clicked()), this, SLOT(changeDB()) );

	connect( mUi->mDatabaseView, SIGNAL(clicked(const QModelIndex&)), this, SLOT(itemClicked(const QModelIndex&)) );
	mUi->mDatabaseImage->setVisible(false);
}

void DatabasePage::changeDB() {
	if (QObject::sender() == mUi->mDatabaseItems) {
		mItemDb = true;
		QSqlTableModel * model = new QSqlTableModel();
		model->setTable("Items");
		model->select();
		model->removeColumn(2);
		delete mUi->mDatabaseView->model();
		mUi->mDatabaseView->setModel(model);
	} else if (QObject::sender() == mUi->mDatabaseFriday) {
		mItemDb = false;
		QSqlQueryModel * model = new QSqlQueryModel();
		model->setQuery("SELECT Codes.Id as Code, Items.Summary, Scanned.Date "
			"FROM Codes INNER JOIN Items ON Codes.ItemId = Items.Id "
			"LEFT OUTER JOIN Scanned ON Codes.Id = Scanned.Code "
			"WHERE Day = 1");
		delete mUi->mDatabaseView->model();
		mUi->mDatabaseView->setModel(model);
	} else if (QObject::sender() == mUi->mDatabaseSaturday) {
		mItemDb = false;
		QSqlQueryModel * model = new QSqlQueryModel();
		model->setQuery("SELECT Codes.Id as Code, Items.Summary, Scanned.Date "
			"FROM Codes INNER JOIN Items ON Codes.ItemId = Items.Id "
			"LEFT OUTER JOIN Scanned ON Codes.Id = Scanned.Code "
			"WHERE Day = 2");
		delete mUi->mDatabaseView->model();
		mUi->mDatabaseView->setModel(model);
	} else if (QObject::sender() == mUi->mDatabaseSunday) {
		mItemDb = false;
		QSqlQueryModel * model = new QSqlQueryModel();
		model->setQuery("SELECT Codes.Id as Code, Items.Summary, Scanned.Date "
			"FROM Codes INNER JOIN Items ON Codes.ItemId = Items.Id "
			"LEFT OUTER JOIN Scanned ON Codes.Id = Scanned.Code "
			"WHERE Day = 3");
		delete mUi->mDatabaseView->model();
		mUi->mDatabaseView->setModel(model);
	}
	mUi->mDatabaseView->resizeColumnsToContents();
}

void DatabasePage::itemClicked(const QModelIndex& index) {
	if (mPopupImage != 0) {
		mPopupImage->hide();
	} else {
		int itemId(index.model()->index(index.row(), 0).data().toInt());

		QSqlQuery query;
		if (mItemDb) {
			query.exec("SELECT Image, Rotate FROM Items WHERE Id = " + QString::number(itemId));
		} else {
			query.exec("SELECT Image, Rotate FROM Items INNER JOIN Codes ON Items.Id = Codes.ItemId WHERE Codes.Id = " + QString::number(itemId));
		}

		query.first();
		QByteArray imageData = query.value(0).toByteArray();
		int rotate = query.value(1).toInt();

		QImage image;
		image.loadFromData(imageData, "PNG");
		image = image.scaledToWidth(300);
		if (rotate > 0) {
			QTransform tran;
			if (rotate == 1) {
				tran.rotate(90); //Klokgewijs
			} else {
				tran.rotate(-90); //Tegen klokgewijs
			}
			image = image.transformed(tran);
		}
		mUi->mDatabaseImage->setVisible(true);
		mUi->mDatabaseImage->setFixedWidth(300);
		mUi->mDatabaseImage->setPixmap(QPixmap::fromImage(image));
	}
}

void DatabasePage::popupHided() {
	delete mPopupImage;
	mPopupImage = 0;
}